import hashlib
import socket
import base64
from redash import settings
from datetime import datetime


def hly_uname():
    host = socket.getfqdn(socket.gethostname())
    addr = socket.gethostbyname(host)
    return host + addr + settings.SQLALCHEMY_DATABASE_URI +  settings.REGISTER["users"] + settings.REGISTER["date"]

def hly_short_uname():
    return settings.SQLALCHEMY_DATABASE_URI +  settings.REGISTER["users"] + settings.REGISTER["date"]

def hly_hash_password(symbol, pws):
    metastr = symbol + "728304" + hly_short_uname()
    #metastr = "COMMON728304postgresql://postgres:123456@127.0.0.1:5432/postgres02023-12-31"
    #localhost.localdomain127.0.1.1postgresql://postgres:123456@127.0.0.1:5432/postgres02023-12-31
    shastr = metastr.encode('gbk')
    sha = hashlib.sha1(shastr).hexdigest()
    if sha != pws:
        metastr = symbol + "728304" + hly_uname()
        shastr = metastr.encode('gbk')
        sha = hashlib.sha1(shastr).hexdigest()    
    return True if sha == pws else False 

def hly_password():
    if not hly_hash_password("COMMON",settings.REGISTER["common"]): return False

    if settings.REGISTER["date"] == "": return True

    today = datetime.now().date()
    expire = datetime.strptime(settings.REGISTER["date"], '%Y-%m-%d').date()
    return True if today <= expire else False
